package cn.schoolwow.quickdao.test.module.business.user.dml.user.flow.edit.save;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.database.dml.UpdateType;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.junit.Assert;

import java.util.List;

public class SaveUserFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        singleSave(flowContext);
        multipleSave(flowContext);
    }

    @Override
    public String name() {
        return "保存用户";
    }

    private void singleSave(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        User insertUser = (User) flowContext.checkData("insertUser");

        insertUser.setPassword("333333");
        int effect = dao.updateType(UpdateType.UpdateById).save(insertUser);
        Assert.assertEquals(1, effect);
    }

    private void multipleSave(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        List<User> insertUserList = (List<User>) flowContext.checkData("insertUserList");

        for(User user:insertUserList){
            user.setPassword("654321");
        }
        for(int i=11;i<=20;i++){
            User user = new User();
            user.setUsername("user"+i);
            user.setPassword("123456");
            user.setEmail("user"+i+"@fake.com");
            insertUserList.add(user);
        }

        int effect = dao.updateType(UpdateType.UpdateByUniqueKey).save(insertUserList);
        Assert.assertEquals(insertUserList.size(), effect);
    }
}
